#include <click/config.h>
#include <click/confparse.hh>
#include <click/error.hh>
#include "Reserver.hh"

CLICK_DECLS

#define TIMERCYCLE 8000

Reserver::Reserver(): timer(this) {
	timer.initialize(this);
}

Reserver::~Reserver() {
}

int Reserver::configure(Vector<String> &conf, ErrorHandler *errh) {
	if (cp_va_kparse(conf, this, errh, cpEnd) < 0) return -1;
	
	return 0;
}

void Reserver::run_timer(Timer* t) {
	click_chatter("Router closed reservation!");
}

void Reserver::push(int value, Packet *p) {
	WritablePacket* q = p->uniqueify();
    click_ip* ipHeader = reinterpret_cast<click_ip *>(q->data());
	
	if (ipHeader->ip_p == IP_PROTO_RSVP) {
		RsvpHeader* rsvpHeader = (RsvpHeader*) (ipHeader+1);
		if (rsvpHeader->type == 1) {
			click_chatter("Router got Path message.");
		} else if (rsvpHeader->type == 2) {
			click_chatter("Router got Resv message.");
			timer.schedule_after_ms(TIMERCYCLE);
		} else if (rsvpHeader->type == 5) {
			click_chatter("Router got Teardown message.");
			timer.clear();
		}
		output(0).push(p);
	} else {
		if (timer.scheduled()) {
			output(1).push(p);
		}

	}
}

CLICK_ENDDECLS
EXPORT_ELEMENT(Reserver)
